package cn.hh.wechatkit.helper;

import cn.hh.wechatkit.api.jssdk.WxAPI_JsApi_Ticket_Api;
import cn.hh.wechatkit.api.token.WxAPI_APITicket_Token_Api;
import cn.hh.wechatkit.api.token.WxAPI_Access_Token_Api;
import cn.hh.wechatkit.api.token.WxAPI_Autho_Access_token_Api;
import cn.hh.wechatkit.exception.Wx_Exception_GetTokenFailed;
import cn.hh.wechatkit.exception.Wx_Exception_InvalidJsonParse;
import cn.hh.wechatkit.exception.Wx_Exception_InvalidResponse;
import cn.hh.wechatkit.param.WxParam;
import cn.hh.wechatkit.pojo.token.WxToken_AccessToken;
import cn.hh.wechatkit.pojo.token.WxToken_ApiTicketToken;
import cn.hh.wechatkit.pojo.token.WxToken_AuthoAccessToken;
import cn.hh.wechatkit.pojo.token.WxToken_JsApiToken;
import lombok.Setter;

import java.io.IOException;

/**
 * Created this one by HMH on 2017/5/23.
 * 帮助类，获取accesstoken，等token的方法都放在这儿
 */

@Setter
public class CacheHelper {
    private static WxToken_JsApiToken jsApiToken;
    private static WxToken_AccessToken accessToken;
    private static WxToken_AccessToken appAccessToken;
    private static WxToken_ApiTicketToken apiTicketToken;

    public static WxToken_AuthoAccessToken getAuthoAccessToken(String code) throws Wx_Exception_GetTokenFailed, Wx_Exception_InvalidJsonParse, Wx_Exception_InvalidResponse, IOException {
        //以后可以缓存
        WxToken_AuthoAccessToken authoAccessToken = WxAPI_Autho_Access_token_Api.getToken(WxParam.appId, WxParam.appSecret, code);
        if (authoAccessToken.hasError()) {
            throw new Wx_Exception_GetTokenFailed("获取AuthoAccessToken失败：" + authoAccessToken.getErrmsg());
        }
        return authoAccessToken;
    }

    public static WxToken_ApiTicketToken getApiTicketToken() throws Wx_Exception_GetTokenFailed, Wx_Exception_InvalidJsonParse, Wx_Exception_InvalidResponse, IOException {
        if (apiTicketToken == null || apiTicketToken.isExpired()) {
            WxToken_ApiTicketToken temp = WxAPI_APITicket_Token_Api.getToken(getAccessToken().getAccess_token());
            if (temp == null) {
                throw new Wx_Exception_GetTokenFailed("获取ApiTicketToken失败");
            }
            apiTicketToken = temp;
        }
        return apiTicketToken;
    }

    public static WxToken_JsApiToken getJsApiToken() {
        if (jsApiToken == null || jsApiToken.isExpired()) {
            WxToken_JsApiToken temp = WxAPI_JsApi_Ticket_Api.getToken(getAccessToken().getAccess_token());
            if (temp == null) {
                throw new Wx_Exception_GetTokenFailed("获取JsApiTicket失败");
            }
            jsApiToken = temp;
        }
        return jsApiToken;
    }

    public static WxToken_AccessToken getAccessToken() {
        if (accessToken == null || accessToken.isExpired()) {
            WxToken_AccessToken temp = WxAPI_Access_Token_Api.getAccess_token(WxParam.appId, WxParam.appSecret);
            if (temp == null) {
                throw new Wx_Exception_GetTokenFailed("获取AccessToken失败");
            }
            accessToken = temp;
        }
        return accessToken;
    }

    public static WxToken_AccessToken getAppAccessToken() {
        if (appAccessToken == null || appAccessToken.isExpired()) {
            WxToken_AccessToken temp = WxAPI_Access_Token_Api.getAccess_token(WxParam.miniAppId, WxParam.miniAppSecret);
            if (temp == null) {
                throw new Wx_Exception_GetTokenFailed("获取AccessToken失败");
            }
            appAccessToken = temp;
        }
        return appAccessToken;
    }


}
